package com.ruoyi.filter;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class SensitiveRequestWrapper extends HttpServletRequestWrapper {

    private final SensitiveWordBs sensitiveWordBs;
    private final byte[] rawData;
    public SensitiveRequestWrapper(HttpServletRequest request, SensitiveWordBs sensitiveWordBs) throws IOException {
        super(request);
        this.sensitiveWordBs = sensitiveWordBs;
        // 读取原始请求体
        InputStream inputStream = request.getInputStream();
        String requestBody = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));

        // 处理敏感词
        String sanitizedBody = replaceSensitiveWords(requestBody);

        // 将处理后的请求体存储为字节数组
        this.rawData = sanitizedBody.getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(rawData);
        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }

            @Override
            public boolean isFinished() {
                return byteArrayInputStream.available() == 0;
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setReadListener(ReadListener listener) {
                throw new UnsupportedOperationException();
            }
        };
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream(), StandardCharsets.UTF_8));
    }

//    private String replaceSensitiveWords(String text, SensitiveWordBs sensitiveWordBs) {
//        if (text == null) return null;
//        return sensitiveWordBs.replace(text);
//    }
//
//    }
//
    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        return replaceSensitiveWords(value);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> parameterMap = super.getParameterMap();
        Map<String, String[]> newMap = new HashMap<>();
        parameterMap.forEach((key, values) -> {
            String[] newValues = new String[values.length];
            for (int i = 0; i < values.length; i++) {
                newValues[i] = replaceSensitiveWords(values[i]);
            }
            newMap.put(key, newValues);
        });
        return newMap;
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (values == null) return null;
        return Arrays.stream(values).map(this::replaceSensitiveWords).toArray(String[]::new);
    }

    private String replaceSensitiveWords(String text) {
        if (text == null) return null;
        return text;
    }


}
